import Sky from './Sky'
import Land from './Land'
import Bird from './Bird'
import PipePareProducer from './Pipe'

// sky需要 skyDom
// land需要 speed  landDom
// bird需要 birdDom, landDom, gameDom(容器)
// pipe需要 speed, landDom, gameDom

class Game {
  constructor(speed, skyDom, birdDom, landDom, gameDom) {
    this.sky = new Sky(skyDom)
    this.land = new Land(speed, landDom)
    this.bird = new Bird(birdDom, landDom, gameDom)
    //柱子对生成器
    this.pipeProducer = new PipePareProducer(speed, landDom, gameDom)
    this.timer = null
    this.tick = 16 //移动时间间隔，毫秒
    this.gameOver = false
  }

  start() {
    if (this.timer) {
      return
    }
    if (this.gameOver) {
      //重新开始游戏
      window.location.reload()
    }
    this.pipeProducer.startProduce() //开始生成柱子
    this.bird.startSwing()
    this.timer = setInterval(() => {
      const duration = this.tick / 1000
      this.sky.move(duration)
      this.land.move(duration)
      this.bird.move(duration)
      this.pipeProducer.pairs.forEach((pair) => {
        pair.move(duration)
      })
      if (this.isGameOver()) {
        // 加个弹出层提示游戏结束
        this.stop()
        this.gameOver = true
      }
    }, this.tick)
  }

  /**
   * 判断两个矩形是否碰撞
   * @param {*} rec1
   * @param {*} rec2
   */
  isHit(rec1, rec2) {
    // 横向：两个矩形的中心点的横向距离，是否小于矩形宽度之和的一半
    // 纵向：两个矩形的中心点的纵向距离，是否小于矩形高度之和的一半
    var centerX1 = rec1.left + rec1.width / 2
    var centerY1 = rec1.top + rec1.height / 2
    var centerX2 = rec2.left + rec2.width / 2
    var centerY2 = rec2.top + rec2.height / 2
    var disX = Math.abs(centerX1 - centerX2) //中心点横向距离
    var disY = Math.abs(centerY1 - centerY2) //中心点总想距离
    if (disX < (rec1.width + rec2.width) / 2 && disY < (rec1.height + rec2.height) / 2) {
      return true
    }
    return false
  }

  isGameOver() {
    if (this.bird.top === this.bird.maxY) {
      //鸟碰到了大地
      return true
    }
    for (let i = 0; i < this.pipeProducer.pairs.length; i++) {
      const pair = this.pipeProducer.pairs[i]
      //看柱子对pair是否跟bird进行了碰撞
      if (this.isHit(this.bird, pair.upPipe) || this.isHit(this.bird, pair.downPipe)) {
        return true
      }
    }
    return false
  }

  stop() {
    clearInterval(this.timer)
    this.timer = null
    this.bird.stopSwing()
    this.pipeProducer.stopProduce()
  }

  // 键盘事件
  regEvent() {
    window.onkeydown = (e) => {
      if (e.key === 'Enter') {
        if (this.timer) {
          this.stop()
        } else {
          this.start()
        }
      } else if (e.key === ' ') {
        this.bird.jump()
      }
    }
  }
}

// var g = new Game()
// g.regEvent()

export default Game
